#include "StdAfx.h"
#include "Layout.h"
#include "MonitorPlatform.h"

CLayout::CLayout(CString strName, DWORD dwID, DWORD dwPlayer, int nPreviewTime)
		: m_strLayoutName(strName)
		, m_dwID(dwID)
		, m_dwPlayer(dwPlayer)
		, m_nPreviewTime(nPreviewTime)
{

}

CLayout::~CLayout(void)
{
}

BOOL CLayout::DeleteChannel(int nPos)
{
	CChannelMapIter it = m_ChannelMap.find(nPos);
	if(it != m_ChannelMap.end())
	{
		CChannel* pChannel = it->second;
		theDBManager->DelLayoutChannel(m_dwID, pChannel->GetSipChannelID());
		m_ChannelMap.erase(it);
		return TRUE;
	}

	return FALSE;
}

BOOL CLayout::AddChannel(int nPos, CChannel* pChannel)
{
	CChannelMapIter it = m_ChannelMap.find(nPos);
	if(it != m_ChannelMap.end())
	{
		DeleteChannel(it->first);
	}

	if(theDBManager->AddLayoutChannel(m_dwID, pChannel->GetSipChannelID(), nPos) > 0)
	{
		m_ChannelMap[nPos] = pChannel;
		return TRUE;
	}
	return FALSE;
}

BOOL CLayout::UpdateChannel(int nOldPos, int nNewPos, CChannel* pChannel)
{
	if(theDBManager->UpdateLayoutChannel(m_dwID, pChannel->GetSipChannelID(), nNewPos))
	{
		m_ChannelMap[nNewPos] = pChannel;
		m_ChannelMap.erase(nOldPos);
		return TRUE;
	}

	return FALSE;
}

int CLayout::GetChannelPos(CChannel* pChannel)
{
	for(CChannelMapIter it = m_ChannelMap.begin(); it != m_ChannelMap.end(); ++it)
	{
		CChannel* pTmp = (CChannel*)it->second;
		if(pTmp->GetSipChannelID() == pChannel->GetSipChannelID())
		{
			return it->first;
		}
	}

	return -1;
}

BOOL CLayout::DeleteLayout()
{
	return theDBManager->DelLayout(m_dwID);
}
